import { WebSocketServer, WebSocket } from "ws";
import { Connection } from "./Connection";
import { ApiMsgEnum, IModel } from "../Common";
import { EventEmitter } from "stream";

export class MyServer extends EventEmitter {
    port: number;
    wss: WebSocketServer;

    apiMap: Map<ApiMsgEnum, Function> = new Map();
    
    connections: Set<Connection> = new Set();
    constructor({ port }: { port: number }) {
        super();
        this.port = port;
    }

    start() {
        return new Promise((resolve, reject) => {
            this.wss = new WebSocketServer({
                host: "192.168.18.166",
                port: 9876,
            });

            this.wss.on("listening", () => {
                resolve(true);
            })
            this.wss.on("close", () => {
                reject(false)
            })
            this.wss.on("error", (e) => {
                reject(e)
            })
            this.wss.on("connection", (ws: WebSocket) => {
                const connection = new Connection(this, ws);
                this.connections.add(connection);
                this.emit("connection", connection)//分发事件 连接上了

                connection.on("close", () => {
                    this.connections.delete(connection);
                    this.emit("disconnection",connection)//分发事件 走了

                })
            })
        })
    }

    serApi<T extends keyof IModel["api"] >(name:T,cb:(connection:Connection,args:IModel["api"][T]["req"])=>void){
        this.apiMap.set(name,cb);
    }

}

